package string_question.leecode.test22;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 20:10
 */
public class Solution2 {

    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        getAns(res, "", 0, 0, n);
        return res;
    }

    private void getAns(List<String> res, String path, int left, int right, int n) {
        if (path.length() == n * 2) {
            res.add(path);
        }
        if (left < n) {
            getAns(res, path + '(', left + 1, right, n);
        }
        if (left > right) {
            getAns(res, path + ')', left, right + 1, n);
        }
    }
}
